package 字符串.验证回文子串2;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/8/16 17:19
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        String str = "bca";
        System.out.println(validPalindrome(str));
    }

    //双指针
    public static boolean validPalindrome(String s) {
        if (s.length() == 1) {
            return true;
        }
        int count = 1;
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (Character.toLowerCase(s.charAt(left)) == Character.toLowerCase(s.charAt(right))) {
                left++;
                right--;
            } else {
                if (left == right-1) {
                    return true;
                }
                if (count != 1) {
                    return false;
                } else {
                    if ((Character.toLowerCase(s.charAt(left+1)) == Character.toLowerCase(s.charAt(right))) ||
                            (Character.toLowerCase(s.charAt(left)) == Character.toLowerCase(s.charAt(right-1)))) {
                        count--;
                    }
                }
            }
        }
        return true;
    }
}
